package com.example.kehoobackend.controller;


import com.example.kehoobackend.common.CommonResponse;
import com.example.kehoobackend.domain.Collection;
import com.example.kehoobackend.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@CrossOrigin
@RestController
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    /**
     * 获取用户的收藏
     *
     * @param user_id 用户id
     * @return CommonResponse<List < Collection>>
     */
    @GetMapping("/collection")
    public CommonResponse<List<Collection>> allCollection(
            @RequestParam("user_id") int user_id) {
        return CommonResponse.createForSuccess("收藏列表", collectionService.allCollection(user_id));
    }


    /**
     * 获取用户收藏数
     *
     * @param user_id 用户id
     * @return CommonResponse<Integer>
     */
    @GetMapping("/collection/count")
    public CommonResponse<Integer> countUserCollection(
            @RequestParam("user_id") int user_id) {
        return CommonResponse.createForSuccess("收藏数", collectionService.countUserCollection(user_id));
    }

    /**
     * 添加收藏
     * @param user_id 用户id
     * @param answer_id 回答id
     * @return CommonResponse<Boolean>
     */
    @PostMapping("/collection")
    public CommonResponse<Boolean> addCollection(
            @RequestParam("user_id") int user_id,
            @RequestParam("answer_id") int answer_id) {
        return collectionService.addCollection(user_id, answer_id)
                ?  CommonResponse.createForSuccess("收藏成功", true)
                : CommonResponse.createForSuccess("收藏失败", false);
    }


    /**
     * 取消收藏
     * @param collection_id 用户id
     * @return CommonResponse<Boolean>
     */
    @DeleteMapping("/collection")
    public CommonResponse<Boolean> deleteCollection(
            @RequestParam("user_id") Integer user_id,
            @RequestParam("answer_id") Integer answer_id) {
        return collectionService.deleteCollection(user_id,answer_id)
                ? CommonResponse.createForSuccess("取消收藏成功", true)
                : CommonResponse.createForSuccess("取消收藏失败", false);
    }

}
